<%-- 
    Document   : myProfile
    Created on : 02 6, 12, 1:49:21 PM
    Author     : pauld
--%>

<%@page import="java.io.UnsupportedEncodingException"%>
<%@page import="java.security.NoSuchAlgorithmException"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="Controller.UserManager"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <%
        String user = (String)session.getAttribute("user");
        String name = (String)session.getAttribute("name");
        
        //checks the user type
        if(user == null || 
                (UserManager.isAdmin(user) != 0 || 
                UserManager.isFinanceManager(user) != 0 || 
                UserManager.isProductManager(user) != 0) ) {
            
            //if the user is not logged in or 
            //is a manager/admin redirect to the index page
            response.sendRedirect("index.jsp");
            
        }
        //if the user is logged in and is not an admin/manager
        else if(user != null &&
                (UserManager.isAdmin(user) == 0 &&
                UserManager.isFinanceManager(user) == 0 &&
                UserManager.isProductManager(user) == 0) ){
            
            //if the user is logged in then check for session integrity
            String currSession = request.getRemoteAddr().concat( session.getId() );
            try{
                if( !UserManager.checkSessionIntegrity(currSession, session) ){
                    UserManager.logout(request);
                    response.sendRedirect("login.jsp");
                }
            }catch(NoSuchAlgorithmException e1){
                UserManager.logout(request);
                response.sendRedirect("login.jsp");
            }
            catch(UnsupportedEncodingException e1){
                UserManager.logout(request);
                response.sendRedirect("login.jsp");
            }
            catch(Exception e1){
                UserManager.logout(request);
                response.sendRedirect("login.jsp");
            }
            
        }
    %>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        
        <link rel="stylesheet" href="css/960_24_col.css" />
        <link rel="stylesheet" href="css/reset.css" />
        <link rel="stylesheet" href="css/text.css" />
        <link rel="stylesheet" href="css/style.css" />
        
        <script type="text/javascript" src="scripts/jquery.js"></script>
        <script type="text/javascript" src="scripts/modalScripts.js"></script>
        <script type="text/javascript" src="scripts/formScripts.js"></script>
        
        <title>FooBar Online Bookstore | My Profile</title>
    </head>
    <body>
        <div id="bar"></div>
        <!-------------------------------header here----------------------->
            <div id="header-wrap">
                <div id="dashboard" class="container_24">
                    <div id="dashboard-logo" class="grid_7">
                        <a href="index.jsp"><img src="images/logo.png" alt="logo" /></a>
                    </div>
                    <div id="dashboard-menu" class="grid_17">
                        <ul class="menu" id="main-menu">
                            <li><a href="index.jsp">Home</a></li>
                            <li><a href="main.jsp">Products</a>
                                <ul style="display: none">
                                    <li><a href="main.jsp?show=book">Books</a></li>
                                    <li><a href="main.jsp?show=mags">Magazines</a></li>
                                    <li><a href="main.jsp?show=cd">CD</a></li>
                                    <li><a href="main.jsp?show=dvd">DVD</a></li>
                                    <li><a href="main.jsp?show=acc">Accessories</a></li>
                                </ul>
                            </li>
                            <%
                            if( user != null && UserManager.isAdmin(user) == 0 && UserManager.isFinanceManager(user) == 0 && UserManager.isProductManager(user) == 0 ){
                                out.println("<li><a href='myProfile.jsp'>My Profile</a></li>");
                            }
                            else if(UserManager.isAdmin(user) == 1){
                                out.println("<li><a href='admin/adminPanel.jsp'>Admin Panel</a></li>");
                            }
                            else if(UserManager.isFinanceManager(user) == 1){
                                out.println("<li><a href='admin/financePanel.jsp'>Sales Panel</a></li>");
                            }
                            else if(UserManager.isProductManager(user) != 0){
                                out.println("<li><a href='admin/productPanel.jsp'>Product Panel</a></li>");
                            }
                            %>
                            <li><a href="about.jsp">About</a></li>
                            <li><a href="contact.jsp">Contact Us</a></li>
                            <%
                            if(user == null){
                                out.println("<li><a href='login.jsp'>Login</a> | <a href='register.jsp'>Join Now</a></li>");
                            }
                            else{
                                out.println("<li><form action='logout' name='logout' method='post'>"
                                + "<a id='user-header' href='myProfile.jsp'>" + name + "</a>"
                                + "<input type='submit' value='Logout' class='submit-button' />"
                                + "</form></li>");
                            }
                            %>
                        </ul>
                    </div>
                </div>
            </div>
        <!-------------------------------content here----------------------->
        <div id="content-wrap">
            <div id="content" class="container_24">
                <div id="profile-info" class="grid_12">
                <%
                //get the user info of the currently logged in user
                //then print out the results in a form
                ResultSet userInfo = UserManager.getUserInfo( user );

                while (userInfo.next()) {
                out.println("<form action='editUser' method='post' id='profile-form' class='styled-form'>" +
                    "<h1>" + userInfo.getString(2) + "</h1>" +
                    "<a class='modal-trigger box-type' href='change-password-modal'>Change Password</a><br /><br />" +
                    "<label>Last Name: </label><br />"
                    + "<input type='text' id='profile-lname' name='lname' value='" + userInfo.getString(3) + "' class='form-inputs' maxlength='25' /><br />" +
                    "<label>First Name: </label><br />"
                    + "<input type='text' id='profile-fname' name='fname' value='" + userInfo.getString(4) + "' class='form-inputs' maxlength='25' /><br />" +
                    "<label>Middle Initial: </label><br />"
                    + "<input type='text' id='profile-minitial' name='mname' value='" + userInfo.getString(5) + "' class='form-inputs' maxlength='1' /><br />" +
                    "<label>E-mail: </label><br />"
                    + "<input type='text' id='profile-email' name='email' value='" + userInfo.getString(6) + "' class='form-inputs' maxlength='25' /><br />" +
                    "<label>Address: </label><br />"
                    + "<textarea type='text' id='profile-add' name='add' class='form-inputs' cols='30' rows='10' maxlength='45'>" + userInfo.getString(7) + "</textarea><br />" +
                    "<label>Country: </label><br />"
                    + "<select name='country' id='profile-country'>"
                    + "<option value='Philippines'>Philippines</option>"
                    + "</select><br />"
                    + "<label>Zip: </label><br />"
                    + "<input type='text' id='profile-zip' name='zip' value='" + userInfo.getString(9) + "' class='form-inputs' maxlength='4' /><br />" +
                    "<input type='submit' name='submit' value='Save' class='submit-button' />" +
                    "<input type='reset' name='reset' value='Reset' class='submit-button' />"+
                    "</form>");
                }
                
                //close the database connection
                UserManager.closeDB();
                %>
                </div>
                <div class="grid_12">
                    <ul class="error-pane" id="profile-error-pane">
                        <%
                        //display pane for the errors
                        try{
                            //displays 'Changes Saved' when the changes to the user was saved
                            if( request.getParameter("saved").equals("1") ){
                                out.println("<li class='important success'>Changes Saved</il>");
                            }
                            else if( request.getParameter("saved").equals("0") ){
                                out.println("<li class='important'>Please Complete The Form Correctly</li>");
                            }
                            else if( request.getParameter("saved").equals("2") ){
                                out.println("<li class='important'>Oops! Something Is Wrong Please Try Again Later..</li>");
                            }
                            else if( request.getParameter("saved").equals("3") ){
                                out.println("<li class='important success'>Password Changed</li>");
                            }
                            else if( request.getParameter("saved").equals("4") ){
                                out.println("<li class='important'>Your Old Password Did Not Match</li>");
                            }
                            else if( request.getParameter("saved").equals("5") ){
                                out.println("<li class='important'>Please Check Your New Password</li>");
                            }
                        }
                        catch(java.lang.NullPointerException e1){
                            //catches the exception whrein if the parameter 'saved' does not yet exist
                        }
                        %>
                        <li style="display: none;" id='profile-error-email'>Please enter a valid e-mail address</li>
                        <li style="display: none;" id='profile-error-name'>Your name is too long</li>
                        <li style="display: none;" id='profile-error-add'>Please enter your address</li>
                        <li style="display: none;" id='profile-error-country'>Please select a country</li>
                        <li style="display: none;" id='profile-error-zip'>Please enter a valid zip code</li>
                    </ul>
                </div>
                
                <div id="profile-collection" class="grid_24">
                    <h1>My Collection</h1>
                    <%
                    //get all the products bought by the user
                    //then print it out
                    ResultSet userBought = UserManager.getUserBought( user );
                    
                    
                    out.println("<ul id='collection-list'>");
                    while( userBought.next() ){
                        out.println( "<li>"
                                + "<div class='collection-pic'></div>"
                                + "<div class='collection-info'>"
                                + "<p class='title'>" + userBought.getString(5) + "</p>"
                                + "<p class='time'>Bought on: " + userBought.getDate(6) + "</p>"
                                + "</div>"
                                + "</li>" );
                    }
                    out.println("</ul>");
                    %>
                </div>
            </div>
        </div>
        <!-------------------------------footer here----------------------->
        <div id="upper-footer-wrap">
            <div id="upper-footer" class="container_24">
                <div class="grid_7 prefix_17">
                    <ul class="menu">
                        <li><a href="#">top</a></li>
                        <li><a href="index.jsp">home</a></li>
                        <li><a href="contact.jsp">contact us</a></li>
                    </ul>
                </div>
            </div>
        </div>
        <div id="lower-footer-wrap">
            <div id="lower-footer" class="container_24">
                <center>
                    <p>FooBar Online Bookstore SECPROG Project</p>
                </center>
            </div>
        </div>
        
        <div class="modal-box" id="change-password-modal">
            <form action="changePW" method="post" id="changePW-form" class="styled-form">
                <h1>Change Password:</h1>
                <label>Current Password</label>
                <br />
                <input type="password" name="oldPW" class="form-inputs" maxlength="25" />
                <br />
            
                <label>New Password</label>
                <br />
                <input type="password" name="newPW1" class="form-inputs" maxlength="25" />
                <br />
            
                <label>Confirm New Password</label>
                <br />
                <input type="password" name="newPW2" class="form-inputs" maxlength="25" />
                <br />
            
                <input type="submit" value="Change Password" class="submit-button" />
            </form>
        </div>
        <div class="modal-bg"></div>
    </body>
</html>
